package cn.edu.lcu.cs.javaprogramming.thread.ticketoffice.runnable;

import java.util.Random;

/**
 * 售票窗口
 *
 * @author ling
 * @date 2022/5/30 16:10
 */
public class TicketWindow implements Runnable {
    private int remainingTicket;

    public TicketWindow(int remainingTicket) {
        this.remainingTicket = remainingTicket;
    }

    @Override
    public void run() {
        Random random = new Random();
        while (remainingTicket > 0) {
            System.out.println(Thread.currentThread().getName() + " 正在发售第 " + remainingTicket-- + " 张票");
            try {
                Thread.sleep(random.nextInt(2000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        System.err.println(Thread.currentThread().getName() + " 票已售罄！");
    }
}
